import random

from yysautomatic.log import get_logger
from yysautomatic.core.script import Script
from .listener import click_jiesuan

logger = get_logger(__name__)


class JieJieTuPoScript(Script):
    """
    结界突破
    """
    name = '结界突破'
    __count_total = 0
    __count_success = 0
    __count_fail = 0

    def __init__(self, frame_manager):
        self.__frame_manager = frame_manager

    def start(self):
        logger.debug('结界突破脚本启动...')
        frames = self.__frame_manager.frames
        for frame in frames:
            frame.label_jiesuan1.on_show(self.on_jiesuan_show)
            frame.label_jiesuan2.on_show(self.on_jiesuan_show)
            frame.label_jiesuan3.on_show(self.on_jiesuan_show)
            frame.label_jiesuan4.on_show(self.on_jiesuan_show)
        logger.info('结界突破脚本启动成功...')

    def stop(self):
        logger.debug('结界突破脚本停止...')
        frames = self.__frame_manager.frames
        for frame in frames:
            frame.label_jiesuan3.remove_listener(self.on_jiesuan_show)
            frame.label_jiesuan4.remove_listener(self.on_jiesuan_show)
        logger.info('结界突破脚本停止成功...')

    def on_jiesuan_show(self, event):
        logger.info('魂土挑战成功')
        click_jiesuan(event)
        self.__count_success += 1
